package com.gusha.app;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gusha.dao.ProcessingOrderDao;
import com.gusha.dao.SysProcessingDao;
import com.gusha.model.ProcessingOrder;
import com.gusha.model.SysMailing;
import com.gusha.model.SysOrder;
import com.gusha.model.SysProcessing;
import com.gusha.model.SysShopping;
import com.gusha.model.User;
import com.gusha.service.SysProcessingService;
import com.gusha.utils.Constants;
import com.gusha.utils.JsonResult;
import com.gusha.utils.WebValueUtil;

@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
@RequestMapping("/app/processing")
public class AppProcessingController {

	@Autowired
	SysProcessingDao sysProcessingDao;

	@Autowired
	SysProcessingService sysProcessingService;

	@Autowired
	ProcessingOrderDao processingOrderDao;

	// 加工商城列表
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping("/toProcessingList")
	@ResponseBody
	public JsonResult processing(Model model, HttpServletRequest request) {
		JsonResult result = new JsonResult(false);
		String pageIndex = WebValueUtil.getWebValue(request, "pageIndex");
	//	String id = WebValueUtil.getWebValue(request, "id");
		int pageNo = 0;
		if (StringUtils.isNotBlank(pageIndex)) {
			pageNo = Integer.parseInt(pageIndex);
		}
		SysProcessing sysProcessing = new SysProcessing();
//		if (StringUtils.isNotBlank(id)) {
//			sysProcessing.setId(new BigInteger(id));
//		}
		Sort sort = new Sort(Sort.Direction.DESC, "addTime");
		Pageable pageable = PageRequest.of(pageNo, Constants.DEFAULT_PAGESIZE, sort);
		Page<SysProcessing> page = sysProcessingService.findAll(pageable, sysProcessing);

		result.setMsg("查询列表成功");
		result.setObj(page);
		result.setFlag(true);
		return result;

	}

	// 加工商城订单
	@CrossOrigin(origins = "*", maxAge = 3600)
	@PostMapping("/toProcessingOrder")
	@ResponseBody
	public JsonResult toProcessingOrder(Model model, HttpServletRequest request) {
		JsonResult result = new JsonResult(false);

//	 str=[{"processingId":"1","buyNum":"5","userId":"17","receivingAddress":"9"},{"assetsId":"8","finishedNum":"5","userId":"17","receivingAddress":"9"}]
		String processingId = WebValueUtil.getWebValue(request, "processingId");
		String buyNum = WebValueUtil.getWebValue(request, "buyNum");
		String userId = WebValueUtil.getWebValue(request, "userId");

		User user = new User();
			SysProcessing sysProcessing = new SysProcessing();
			ProcessingOrder processingOrder = new ProcessingOrder();

			sysProcessing.setId(new BigInteger(processingId));
			processingOrder.setProcessingId(sysProcessing); // 设置 加工产品ID

			user.setId(new BigInteger(userId));
			processingOrder.setUserId(user);// 设置 购买用户ID
			processingOrder.setBuyNum(new BigInteger(buyNum)); // 设置购买数量
			processingOrder.setState(new BigInteger("0")); // 未购买状态

			processingOrderDao.saveAndFlush(processingOrder);
		
		

		result.setObj(processingOrder);
		result.setMsg("订单生成完成");
		result.setFlag(true);
		return result;
	}

	// 加工商品订单 确认
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping("/toConfirmProcessingOrder")
	@ResponseBody
	public JsonResult toConfirmProcessingOrder(Model model, HttpServletRequest request) {
		JsonResult result = new JsonResult(false, "操作失败");
		String id = WebValueUtil.getWebValue(request, "id");
//		User user = new User();
//		user.setId(new BigInteger(userId));
		ProcessingOrder processingOrder = processingOrderDao.findAllById(new BigInteger(id));
		
		processingOrder.setState(new BigInteger("1")); // 已购买状态
			Date date = new Date();
			processingOrder.setAddTime(date);// 购买时间
			processingOrderDao.saveAndFlush(processingOrder);
			
			 SysProcessing sysProcessing = processingOrder.getProcessingId();
			
			SysProcessing sp = sysProcessingDao.findAllById(sysProcessing.getId());
		//	if (sp.getTradeNum() != null) {
				int num = sp.getTradeNum().intValue(); // 获取加工商城表 交易量
				num += processingOrder.getBuyNum().intValue();// 累加 新的交易量
				sp.setTradeNum(new BigInteger(String.valueOf(num)));// 设置新的交易量
		//	if (sp.getProcessingNum() != null) {
				int a = sp.getProcessingNum().intValue();
				int b = processingOrder.getBuyNum().intValue();
				if (a >= b) {
					sp.setProcessingNum(new BigInteger(String.valueOf(a - b)));// 设置新的库存数量
			//	}
			}
			sysProcessingDao.saveAndFlush(sp);

		
		result.setMsg("购买成功");
		// result.setObj(list2);
		result.setFlag(true);
		return result;
	}
}
